﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.CodeDom.Compiler;
using NBusiness.CodeDom;
using Spark;
using System.IO;
using System.Reflection;

namespace NBusiness.Templates.Engines.Spark
{
    public class SparkEngine : ICodeGeneratorEngine
    {
        #region ICodeGeneratorEngine Members

        public string Generate(Entity entity, string template, EntityCompileParameters parameters)
        {
            NBusinessViewFolder viewFolder = new NBusinessViewFolder();
            viewFolder.Add("template", template);

            SparkViewEngine engine = new SparkViewEngine(
                typeof(NBusinessView).FullName,
                viewFolder);

            LoadReferences();
            SparkViewDescriptor descriptor = new SparkViewDescriptor();
            descriptor.Templates.Add("template");
            NBusinessView view = (NBusinessView)engine.CreateInstance(descriptor);
            view.Entity = entity;
            
            StringBuilder builder = new StringBuilder();
            using (TextWriter writer = new StringWriter(builder))
                view.RenderView(writer);

            return builder.ToString();
        }

        private void LoadReferences()
        {
            string camelCase = Utilities.StringUtil.ToCamelCase("CamelCase");
        }

        #endregion
    }
}
